-- 处理器
-- Author: Colocasia
-- Date: 2018-05-22

local skynet = require "skynet";
local us = require "us";
local table = require "us.table"
local class = require "class";
local code_const = require "code_const";


---@class handler
local M = class("handler");

local table_merge = table.merge;

function M:ctor()
    -- self.__data = {}             -- mark:暂时去掉该字段 没想到有什么作用
end

function M:render(o, code, opmsg, ...)  
    o.code = code or {
        opcode = 0,
        opmsg = opmsg,
        opargs = {...},
    };
    return o
end

function M:code(key, opmsg, ...)
    local code_data = code_const[key];
    if code_data then
        return self:render({}, {opcode = code_data.opcode, opmsg = opmsg, opargs = {...}});
    else
        return self:render({}, {opcode = 99999});
    end
end


-- 执行服务器节点命令 -----------------------------------------------------------------
function M:exc_node_cmd(server, service, ...)
    return us:call(server, service, ...);
end 

function M:send_node_cmd(server, service, ...)
    return us:send(server, service, ...);
end
-------------------------------------------------------------------------------


-- 执行服务器命令 -----------------------------------------------------------------
function M:exc_server_cmd(server, cmd, ...)
    return self:exc_node_cmd(server, 'server_mgr', cmd, ...);
end 

function M:send_server_cmd(server, cmd, ...)
    return self:send_node_cmd(server, 'server_mgr', cmd, ...);
end
-------------------------------------------------------------------------------


-- 执行DB命令 -----------------------------------------------------------------
function M:exc_db_cmd(module, cmd, ...)
    return us:call("DB", "us_dbmgr", module, cmd, ...);
end 

function M:send_db_cmd(module, cmd, ...)
    return us:send("DB", "us_dbmgr", module, cmd, ...);
end
-------------------------------------------------------------------------------


-- 执行匹配命令 -----------------------------------------------------------
function M:exc_match_cmd(server, cmd, ...)
    return self:exc_node_cmd(server, 'match', cmd, ...);
end 

function M:send_match_cmd(server, cmd, ...)
    return self:send_node_cmd(server, 'match', cmd, ...);
end
-------------------------------------------------------------------------------


-- 执行player命令 -----------------------------------------------------------
function M:exc_player_cmd(addr, cmd, ...)
    return skynet.call(addr, 'lua', cmd, ...)
end 

function M:send_player_cmd(addr, cmd, ...)
    return skynet.send(addr, 'lua', cmd, ...)
end
-------------------------------------------------------------------------------


-- 执行room_mgr命令 -----------------------------------------------------------
function M:exc_room_mgr_cmd(cmd, ...)
    return skynet.call('room_mgr', 'lua', cmd, ...)
end 

function M:send_room_mgr_cmd(cmd, ...)
    return skynet.send('room_mgr', 'lua', cmd, ...)
end
-------------------------------------------------------------------------------


-- 执行room命令 ---------------------------------------------------------------
function M:exc_room_cmd(addr, cmd, ...)
    return skynet.call(addr, 'lua', cmd, ...)
end 

function M:send_room_cmd(addr, cmd, ...)
    return skynet.send(addr, 'lua', cmd, ...)
end
-------------------------------------------------------------------------------

return M;
